import passport from 'koa-passport'
import LocalStrategy from 'passport-local'
import mongoUser from '../../dbs/models/users'

passport.use(
  new LocalStrategy(async (username, password, done) => {
    let result = await mongoUser.findOne({ username })
    if (result == null) return done(null, false, '用户不存在')
    if (result.password !== password) return done(null, false, '密码错误')
    return done(null, result)
  })
)

passport.serializeUser(function(user, done) {
  done(null, user) //序列化
})

passport.deserializeUser(function(user, done) {
  return done(null, user) //反序列化
})

export default passport
